package org.example.twoNumberAdd;

import java.util.*;

/**
 * 两数相加之和
 */
public class TwoNumberAdd {
    public static List<int[]> twoSum(int[] nums, int target) {
        List<int[]> result = new ArrayList<>();

        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                result.add(new int[]{nums[map.get(target - nums[i])], nums[i]});
            }
            map.put(nums[i], i);
        }
        return result;
    }

    public static void main(String[] args) {
        StringBuffer str = new StringBuffer();
        List<int[]> result = twoSum(new int[]{1, 2, 4, 8, 9}, 10);
        for (int i = 0; i < result.size(); i++) {
            str.append(Arrays.toString((result.get(i))) + "\t");
        }
        System.out.println(str);
    }
}
